package com.changgou.business.service.impl;

import com.changgou.business.dao.SecondsMapper;
import com.changgou.business.dao.TimeMapper;
import com.changgou.business.pojo.Seconds;
import com.changgou.business.pojo.Time;
import com.changgou.business.service.SecondsService;
import com.changgou.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class SecondsServiceImpl implements SecondsService {

    @Autowired
    private SecondsMapper secondsMapper;

    @Autowired
    private TimeMapper timeMapper;
    //秒杀商品信息更新
    @Override
    public Integer add(Seconds seconds) {

        return secondsMapper.updateByPrimaryKeySelective(seconds);
    }

    /**
     * 查询秒杀商品列表
     * @param id
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Seconds> findGoodsBySid(Integer id, Integer page, Integer size) {
        Time time = timeMapper.selectByPrimaryKey(id);
        if (page == null || page <= 0){
            page = 1;
        }
        if (size == null || size <= 0){
            size = 10;
        }
        PageHelper.startPage(page,size);
        Page<Seconds> list = secondsMapper.findGoodsBySid(time.getStarttime(),time.getEndtime());
        return new PageResult<>(list.getTotal(),list);
    }

    @Override
    public PageResult<Seconds> findSeckillGoodsByName(String goodsName, Integer page, Integer size) {
        if (page == null || page <= 0){
            page = 1;
        }
        if (size == null || size <= 0){
            size = 10;
        }
        PageHelper.startPage(page,size);
        Page<Seconds> list = null;
        if (goodsName == null || goodsName == "" || goodsName.length()<=0){
            list = (Page<Seconds>) secondsMapper.selectAll();
        }else {
           /* Example example = new Example(Seconds.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andLike("goodsName",goodsName);
            criteria.orLike("articleNumber",goodsName);
            Page<Seconds> list = (Page<Seconds>) secondsMapper.selectByExample(example);*/
           list = secondsMapper.findSeckillGoodsByName(goodsName);
        }
        return new PageResult<Seconds>(list.getTotal(),list);
    }

    @Override
    public void deleteId(Integer id) {
        secondsMapper.deleteByPrimaryKey(id);
    }
}
